(function ($) {
    var $parent = $("#menu-parent");
    var $content = $("#base-content");

    var initMenus = function () {
        $.getJSON("/api/menus/all?type=menu", function (data) {
            buildMenus(data);
        });
    };

    var addListeners = function () {
        var $a = $(".sidebar-nav-link a");

        $.each($a, function(i, item) {
            $(item).unbind("click").bind("click", function(e) {
                var target = $(item).prop("tar");
                $content.prop("src", target);
                e.preventDefault();
                return false;
            });
        });
    };

    var buildMenus = function (datas) {
        $.each(datas, function (i, item) {
            $parent.append(buildMenu(item));
        });

        addListeners();
    };

    var buildMenu = function (data) {
        var $li = $("<li class='sidebar-nav-link'/>");
        var icon = data.icon;
        var $a = $("<a/>")
            .prop("href", "javascript: void(0);")
            .prop("id", data.id)
            .prop("tar", data.url);
        var i = "<li class='sidebar-nav-link-logo " + (icon ? icon : "") + "'/>";

        return $li.append($a.append(i + data.name));
    };

    initMenus();
})(jQuery);